package com.media.selector.media.model

import com.media.selector.media.filter.Filter
import com.media.selector.media.model.entity.MimeType

/**
 * date        ：2020/5/11
 * author      ：蒙景博
 * description ：
 */
class SelectionSpec private constructor() {

    companion object {
        @Volatile
        private var INSTANCE: SelectionSpec? = null
        fun with(): SelectionSpec = INSTANCE ?: synchronized(this){ SelectionSpec().also  { INSTANCE = it }}
        fun clean() = with().reset()
    }

    /**
     * 选择资源类型
     */
    var mimeTypeSet: Set<MimeType>? = null

    /**
     * 选择类型，false:要么选择视频，要么选择图片
     */
    var mediaTypeExclusive = false

    /**
     * 显示类型：是否仅显示一种媒体类型，即图像或者视频。
     */
    var showSingleMimeType = false

    /**
     * 最大选中数量
     */
    var maxSelectable = 0

    /**
     * 最大选中图片数
     */
    var maxImageSelectable = 0

    /**
     * 最大选中视频数
     */
    var maxVideoSelectable = 0

    /**
     * 过滤资源类型
     */
    var filters = mutableListOf<Filter>()

    /**
     * 是否添加相机
     */
    var capture = false

    /**
     * 是否显示预览
     */
    var showPreview = false

    private fun reset(): SelectionSpec {
        mimeTypeSet = null
        mediaTypeExclusive = true
        showSingleMimeType = false
        maxSelectable = 1
        maxImageSelectable = 0
        maxVideoSelectable = 0
        filters.clear()
        capture = false
        showPreview = true
        return this
    }

    fun singleSelectionModeEnabled() = maxSelectable == 1 || maxImageSelectable == 1 && maxVideoSelectable == 1

    fun onlyShowImages() = showSingleMimeType && mimeTypeSet?.let { MimeType.ofImage().containsAll(it) } ?: false

    fun onlyShowVideos() = showSingleMimeType && mimeTypeSet?.let { MimeType.ofVideo().containsAll(it) } ?: false

    fun onlyShowGif() = showSingleMimeType && MimeType.ofGif() == mimeTypeSet
}